using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;
using System.Web.Configuration;

namespace AliCMS.SystemModule.OleDb
{
    public class Utility : OleDbBase, Dal.IUtility
    {
		public int GetInnerIDFromGuid(Guid guid)
		{
			throw new NotImplementedException();
		}
		public Guid GetGuidFromInnerID(object id)
		{
			throw new NotImplementedException();
		}
		public Dictionary<string, object> Get(object id)
		{
			throw new NotImplementedException();
		}
		public void Add(Dictionary<string, object> item)
		{
			throw new NotImplementedException();
		}
		public void Modify(int id, Dictionary<string, object> changes)
		{
			throw new NotImplementedException();
		}
		public void Delete(int id)
		{
			throw new NotImplementedException();
		}
		public DataTable List(int page, int pageSize, string condition, string sort)
		{
			throw new NotImplementedException();
		}
		public int GetRecordCount(string condition)
		{
			throw new NotImplementedException();
		}

		public void BackupDb(string name)
        {
            string root = Manager.MapPath(Manager.GetDataPath(null, null));
            string dir = root + "backup\\" + name;
            if (Directory.Exists(dir))
                Manager.DeleteFolder(dir, true);

            Manager.MakeSureFolderExist(dir);
            File.Copy(root + "system.config", dir + "\\system.config");
        }
		public void RestoreDb(string name)
        {
            string root = Manager.MapPath(Manager.GetDataPath(null, null));
            string dir = root + "backup\\" + name;
            if (!Directory.Exists(dir))
                throw new Exception("lang_backup_directory_not_exist");

            File.Copy(dir + "\\system.config", root + "system.config", true);
        }
		public void CompactDb()
        {
            EndWork();

            //create an inctance of a Jet Replication Object
            object jro = Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine"));
            //filling Parameters array
            //cnahge "Jet OLEDB:Engine Type=5" to an appropriate value
            // or leave it as is if you db is JET4X format (access 2000,2002)
            //(yes, jetengine5 is for JET4X, no misprint here)
            string olddb = Manager.MapPath(Manager.GetDataPath(null, null) + "system.config");
            string newdb = Manager.MapPath(Manager.GetDataPath(null, null) + "temp.mdb");
            string connectionString = Manager.GetConnectionString(0, null);
            object[] paramaters = new object[] { connectionString, connectionString.Replace("system.config", "temp.mdb") + ";Jet OLEDB:Engine Type=5" };

            //invoke a CompactDatabase method of a JRO object
            //pass Parameters array
            jro.GetType().InvokeMember("CompactDatabase", System.Reflection.BindingFlags.InvokeMethod, null, jro, paramaters);
            //database is compacted now
            //to a new file C:\\tempdb.mdw
            //let's copy it over an old one and delete it
            System.IO.File.Delete(olddb);
            System.IO.File.Move(newdb, olddb);
            //clean up (just in case)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(jro);
            jro = null;
        }
    }
}
